/** software/vulnerabilities/:cve > Summary section */

import React from "react";

import { IVulnerability } from "interfaces/vulnerability";
import paths from "router/paths";
import { getPathWithQueryParams } from "utilities/url";

import CustomLink from "components/CustomLink";
import Card from "components/Card";
import DataSet from "components/DataSet";
import TooltipWrapper from "components/TooltipWrapper";
import ProbabilityOfExploit from "components/ProbabilityOfExploit";
import { HumanTimeDiffWithDateTip } from "components/HumanTimeDiffWithDateTip";
import LastUpdatedHostCount from "components/LastUpdatedHostCount";

const baseClass = "software-vuln-summary";

interface ISoftwareVulnSummaryProps {
  vuln: IVulnerability;
  isPremiumTier: boolean;
  teamIdForApi?: number;
}

const SoftwareVulnSummary = ({
  vuln,
  isPremiumTier,
  teamIdForApi,
}: ISoftwareVulnSummaryProps) => {
  const {
    cve,
    details_link,
    cve_description,
    cvss_score,
    epss_probability,
    cisa_known_exploit,
    cve_published,
    created_at,
    hosts_count,
    hosts_count_updated_at,
  } = vuln;

  const hostCountPath = getPathWithQueryParams(paths.MANAGE_HOSTS, {
    vulnerability: cve,
    team_id: teamIdForApi,
  });

  return (
    <Card borderRadiusSize="xxlarge" className={baseClass}>
      <span className={`${baseClass}__header`}>
        <h1>{cve}</h1>
        <span className={`${baseClass}__header__links`}>
          <CustomLink url={details_link} text="Visit NVD page" newTab />
        </span>
      </span>
      {isPremiumTier && cve_description && (
        <div className={`${baseClass}__description`}>{cve_description}</div>
      )}
      <dl className={`${baseClass}__description-list`}>
        {isPremiumTier && (
          <>
            {cvss_score && (
              <DataSet
                title={
                  <TooltipWrapper
                    tipContent="The worst case impact across different environments (CVSS version 3.x base score). This data is reported by the National Vulnerability Database (NVD)."
                    // to match neighboring tooltip wrapper
                  >
                    Severity
                  </TooltipWrapper>
                }
                value={cvss_score}
              />
            )}
            {epss_probability && (
              <DataSet
                title={
                  <TooltipWrapper tipContent="The probability that this vulnerability will be exploited in the next 30 days (EPSS probability). This data is reported by FIRST.org.">
                    Probability of exploit
                  </TooltipWrapper>
                }
                value={
                  <ProbabilityOfExploit
                    probabilityOfExploit={epss_probability}
                    cisaKnownExploit={cisa_known_exploit}
                    // to avoid tooltip wrapper above
                    tooltipPosition="bottom"
                  />
                }
              />
            )}
            {cve_published && (
              <DataSet
                title={
                  <TooltipWrapper
                    tipContent={
                      <>
                        The date this vulnerability was published in the
                        National Vulnerability Database (NVD).
                      </>
                    }
                  >
                    Published
                  </TooltipWrapper>
                }
                value={
                  <HumanTimeDiffWithDateTip
                    timeString={cve_published}
                    tooltipPosition="bottom"
                  />
                }
              />
            )}
          </>
        )}
        <DataSet
          title={
            <TooltipWrapper
              tipContent={
                <>The date this vulnerability first appeared on a host.</>
              }
            >
              Detected
            </TooltipWrapper>
          }
          value={
            <HumanTimeDiffWithDateTip
              timeString={created_at}
              tooltipPosition="bottom"
            />
          }
        />
        <DataSet
          title="Affected hosts"
          value={
            <LastUpdatedHostCount
              hostCount={
                <TooltipWrapper tipContent="View all affected hosts">
                  <CustomLink
                    url={hostCountPath}
                    text={hosts_count.toString()}
                  />
                </TooltipWrapper>
              }
              lastUpdatedAt={hosts_count_updated_at}
            />
          }
        />
      </dl>
    </Card>
  );
};

export default SoftwareVulnSummary;
